package cn.wzptsoft.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.wzptsoft.mall.pojo.Category;
import cn.wzptsoft.mall.pojo.dto.PosterDto;
import cn.wzptsoft.mall.pojo.result.Result;
import cn.wzptsoft.mall.pojo.vo.PosterVo;
import cn.wzptsoft.mall.pojo.vo.saveCategoryVo;
import cn.wzptsoft.mall.service.IPosterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/poster")
@Slf4j
@Tag(name = "广告相关接口")
@RequiredArgsConstructor
public class PosterController {

    private final IPosterService iPosterService;

    @GetMapping("name/{name}")
    @Operation(summary = "查询广告信息")
    public Result<PosterVo> getPoster(@PathVariable String name) {
        log.info("查询广告信息，{}",name);
        PosterVo vo = iPosterService.getPoster(name);
        return Result.ok(vo);
    }

    @PostMapping("/add")
    @Operation(summary = "新增广告")
    @PreAuthorize("hasRole('admin')")
    public Result addPoster(@RequestBody PosterDto posterDto) {
        iPosterService.savePoster(posterDto);
        return Result.ok();
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告")
    @PreAuthorize("hasRole('admin')")
    public Result updatePoster(@RequestBody PosterDto posterDto){

        iPosterService.updatePoster(posterDto);

        return Result.ok();

    }

    @DeleteMapping
    @Operation(summary = "删除广告")
    @PreAuthorize("hasRole('admin')")
    public Result delete(Integer id){
        iPosterService.removeById(id);
        return Result.ok();
    }



}
